//grid通用配置
var defaultGridConFig = {
	queryFrom:"#queryFrom",
	queryFromDiv:"#queryFromDiv",
	gridInit: function(){},
	buttonBind:function(){},
	cleanData:function(){
		var _this = this;
		_this.grid.jqGrid("clearGridData");
	},
	getData:function(){//获取所有行
		var _this = this;
		var datas = [];
		var dataIds = _this.grid.jqGrid("getDataIDs");
		$.each(dataIds,function(i,id){
			var row = _this.getRowData(id);
			if(row){
				datas.push(row);
			}
		});
		return datas;
	},
	leftClickSaveRow:function(gridId){
		  var _this = this;
		 $(document).bind("contextmenu",function(e){
			 e.preventDefault();
	         return false;
		 });
		 $(gridId).bind("mousedown",function(e){
			 var e = e || window.event 
		        if(3 == e.which){  
		        	_this.cancelEditRow();
		        }  
		 });
	},
	gridIsMultiselect:function(){
		return this.grid.jqGrid("getGridParam","multiselect");
	},
	setGridParams:function(params){
		this.grid.jqGrid('setGridParam',params).trigger("reloadGrid");
	},
	gridQuery:function(params){
		//设置查询错误事件---session过期事件
		this.grid.jqGrid('setGridParam',{    
			loadError:function(xhr,status,error){
				var oauthstatus = xhr.getResponseHeader("oauthstatus");
				 if(oauthstatus == '401'){
    	        	 window.location.href = contextPath + "/view.htm";
    	        }
			}
	    });
		
		this.grid.jqGrid('setGridParam',{    
	        datatype:'json',    
	        postData:params,  
	        page:1    
	    }).trigger("reloadGrid");
	},reLoad:function(params){
		//设置查询错误事件---session过期事件
		this.grid.jqGrid('setGridParam',{    
			loadError:function(xhr,status,error){
				var oauthstatus = xhr.getResponseHeader("oauthstatus");
				if (oauthstatus == '401') {
					window.location.href = contextPath + "/view.htm";
				}
			}
	    });
		this.grid.trigger("reloadGrid"); 
	},getSelectRowData:function(){
		 var id = this.grid.jqGrid('getGridParam','selrow');
		 var rowData = false;
		 if(id){
			  rowData = this.grid.getRowData(id);
		 }
		 return rowData;
	}
	,getRowData:function(id){
		 var rowData = false;
		 if(id){
			  rowData = this.grid.jqGrid('getRowData', id);
		 }
		 return rowData;
	},
	existDataId:function(dataId){
		var status = false;
		var dataIds = this.grid.jqGrid("getDataIDs");
		$.each(dataIds,function(i,id){
			if(id == dataId){
				status =  true;
			}
		});
		return status;
	},
	editRow:function(){
	    var _this = this;
	    //取消当前编辑
	    _this.cancelEditRow();
		var id = this.grid.jqGrid('getGridParam','selrow');
		 if(id){
			 //保存当前编辑行ID
			 _this.editRowId = id;
			 _this.grid.jqGrid('editRow', id,{keys:true,url:false});
		 }
	},
	cancelEditRow:function(){
	    var _this = this;
		if( _this.editRowId){
			 _this.grid.jqGrid('saveRow', _this.editRowId,{
				 successfunc:function(){return true;},
				 url:false
			 });
			 _this.editRowId = null;
		}
	},
	addRowData:function(rowId,data){
		var _this = this;
		 _this.grid.jqGrid("addRowData",rowId,data,0);
	},
	deleteRow:function(id){
		var _this = this;
		 if(id){
			 _this.grid.jqGrid('delRowData', id);
		 }
	},
	deleteSelectRow:function(){
		var _this = this;
		 var id = this.grid.jqGrid('getGridParam','selrow');
		 if(id){
			 _this.grid.jqGrid('delRowData', id);
		 }
	},
	getSelectRowsIds:function(){
		//获取多选到的id集合  
		 var ids =  this.grid.jqGrid("getGridParam", "selarrrow");  
		 return ids;
		},
		getSelectRowsData:function(){
			var _this = this;
			var ids =  this.grid.jqGrid("getGridParam", "selarrrow"); 
			var datas = [];
			//遍历访问这个集合  
			$(ids).each(function (index, id){  
			     //由id获得对应数据行  
				var row =  _this.grid.jqGrid('getRowData', id);  
				datas.push(row);
			}); 
			return datas;
		},queryParams:function(){
		var _this = this;
		var params = $(_this.queryFrom).serialize();
		return params;
	},
	doQuery:function(_this,btn){
		//var params = $(_this.queryFrom).serialize();
		var params = {};
		var a =  $(_this.queryFrom).serializeArray();
		$.each(a, function() {
			if (params[this.name] !== undefined) {
				if (!params[this.name].push) {
					params[this.name] = [o[this.name]];
				}
				params[this.name].push(this.value || null);
			} else {
				params[this.name] = this.value || null;
			}
		});
		_this.gridQuery(params);
	},queryFromHeight:function(){
		var _this = this;
		return $(_this.queryFromDiv).height();
	}
}



//ZTREE通用配置
var defaultZtreeConFig = {
		setting:{
		
		},
		treeInit:function(id){//初始化树
			this.tree = $.fn.zTree.init($(id), this.setting);
		},
		getSelectNode:function(){
			if(this.tree){
				var nodes = this.tree.getSelectedNodes();
				if(nodes.length != 1)return false;
				else return nodes[0];
			}
		},
		reloadSelectNode:function(){
			var node = this.getSelectNode();
			if(node){
				this.tree.reAsyncChildNodes(node, "refresh", false);
			}else{
				this.tree.reAsyncChildNodes(null, "refresh", false);
			}
		}
}

/*****************************************************************
jQuery Validate扩展验证方法  (ray)       
*****************************************************************/
$(function(){
// 判断整数value是否等于0 
jQuery.validator.addMethod("isIntEqZero", function(value, element) { 
value=parseInt(value);      
return this.optional(element) || value==0;       
}, "整数必须为0"); 

// 判断整数value是否大于0
jQuery.validator.addMethod("isIntGtZero", function(value, element) { 
value=parseInt(value);      
return this.optional(element) || value>0;       
}, "整数必须大于0"); 

// 判断整数value是否大于或等于0
jQuery.validator.addMethod("isIntGteZero", function(value, element) { 
value=parseInt(value);      
return this.optional(element) || value>=0;       
}, "整数必须大于或等于0");   

// 判断整数value是否不等于0 
jQuery.validator.addMethod("isIntNEqZero", function(value, element) { 
value=parseInt(value);      
return this.optional(element) || value!=0;       
}, "整数必须不等于0");  

// 判断整数value是否小于0 
jQuery.validator.addMethod("isIntLtZero", function(value, element) { 
value=parseInt(value);      
return this.optional(element) || value<0;       
}, "整数必须小于0");  

// 判断整数value是否小于或等于0 
jQuery.validator.addMethod("isIntLteZero", function(value, element) { 
value=parseInt(value);      
return this.optional(element) || value<=0;       
}, "整数必须小于或等于0");  

// 判断浮点数value是否等于0 
jQuery.validator.addMethod("isFloatEqZero", function(value, element) { 
value=parseFloat(value);      
return this.optional(element) || value==0;       
}, "浮点数必须为0"); 

// 判断浮点数value是否大于0
jQuery.validator.addMethod("isFloatGtZero", function(value, element) { 
value=parseFloat(value);      
return this.optional(element) || value>0;       
}, "浮点数必须大于0"); 

// 判断浮点数value是否大于或等于0
jQuery.validator.addMethod("isFloatGteZero", function(value, element) { 
value=parseFloat(value);      
return this.optional(element) || value>=0;       
}, "浮点数必须大于或等于0");   

// 判断浮点数value是否不等于0 
jQuery.validator.addMethod("isFloatNEqZero", function(value, element) { 
value=parseFloat(value);      
return this.optional(element) || value!=0;       
}, "浮点数必须不等于0");  

// 判断浮点数value是否不等于0 
jQuery.validator.addMethod("isValidImg", function(value, element) { 
if(this.optional(element)){
return true;
}

var result ="";
var arr = new Array();
arr = value.split(".");
if(arr.length > 1){
result = arr[1];
}

var image=new Array();
image[0]="jpg";
image[1]="jpeg";
image[2]="bmp";
image[3]="gif";
image[4]="png";
for(var ele in image){
temp=image[ele];
if(temp == result.toLowerCase()){
return true;
}
}

return false;   
}, "请上传 [jpg, jpeg, bmp, gif] 格式的图片");  

// 判断浮点数value是否小于0 
jQuery.validator.addMethod("isFloatLtZero", function(value, element) { 
value=parseFloat(value);      
return this.optional(element) || value<0;       
}, "浮点数必须小于0");  

// 判断浮点数value是否小于或等于0 
jQuery.validator.addMethod("isFloatLteZero", function(value, element) { 
value=parseFloat(value);      
return this.optional(element) || value<=0;       
}, "浮点数必须小于或等于0");  

// 判断浮点型  
jQuery.validator.addMethod("isFloat", function(value, element) {       
return this.optional(element) || /^[-\+]?\d+(\.\d+)?$/.test(value);       
}, "只能包含数字、小数点等字符"); 

// 匹配integer
jQuery.validator.addMethod("isInteger", function(value, element) {       
return this.optional(element) || (/^[-\+]?\d+$/.test(value) && parseInt(value)>=0);       
}, "匹配integer");  

// 判断数值类型，包括整数和浮点数
jQuery.validator.addMethod("isNumber", function(value, element) {       
return this.optional(element) || /^[-\+]?\d+$/.test(value) || /^[-\+]?\d+(\.\d+)?$/.test(value);       
}, "匹配数值类型，包括整数和浮点数");  

// 只能输入[0-9]数字
jQuery.validator.addMethod("isDigits", function(value, element) {       
return this.optional(element) || /^\d+$/.test(value);       
}, "只能输入0-9数字");  

// 判断中文字符 
jQuery.validator.addMethod("isChinese", function(value, element) {       
return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);       
}, "只能包含中文字符。");   

// 判断是否中文姓名
jQuery.validator.addMethod("isChineseName", function(value, element) {       
return this.optional(element) || (/^[\u0391-\uFFE5]+$/.test(value) && (value.length > 1 && value.length < 5));       
}, "请输入正确的姓名");   

// 判断英文字符 
jQuery.validator.addMethod("isEnglish", function(value, element) {       
return this.optional(element) || /^[A-Za-z]+$/.test(value);       
}, "只能包含英文字符。");   

// 手机号码验证    
jQuery.validator.addMethod("isMobile", function(value, element) {    
var length = value.length;    
return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(value));    
}, "请正确填写您的手机号码。");

// 电话号码验证    
jQuery.validator.addMethod("isPhone", function(value, element) {    
var tel = /^(\d{3,4}-?)?\d{7,9}$/g;    
return this.optional(element) || (tel.test(value));    
}, "请正确填写您的电话号码。");

// 联系电话(手机/电话皆可)验证   
jQuery.validator.addMethod("isTel", function(value,element) {   
var length = value.length;   
var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;   
var tel = /^(\d{3,4}-?)?\d{7,9}$/g;       
return this.optional(element) || tel.test(value) || (length==11 && mobile.test(value));   
}, "请正确填写您的联系方式"); 

// 匹配qq      
jQuery.validator.addMethod("isQq", function(value, element) {       
return this.optional(element) || /^[1-9]\d{4,12}$/;       
}, "匹配QQ");   

// 邮政编码验证    
jQuery.validator.addMethod("isZipCode", function(value, element) {    
var zip = /^[0-9]{6}$/;    
return this.optional(element) || (zip.test(value));    
}, "请正确填写您的邮政编码。");  

// 匹配密码，以字母开头，长度在6-12之间，只能包含字符、数字和下划线。      
jQuery.validator.addMethod("isPwdvaidate", function(value, element) {       
return this.optional(element) || /^[a-zA-Z0-9_]{6,12}$/.test(value);       
}, "密码长度在6-12之间，必须包含字母和数字只能包含字符、数字和下划线。");  

// 身份证号码验证
jQuery.validator.addMethod("isIdCardNo", function(value, element) { 
return this.optional(element) || /^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/.test(value);    
}, "请输入正确的身份证号码。"); 

// IP地址验证   
jQuery.validator.addMethod("ip", function(value, element) {    
return this.optional(element) || /^(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))$/.test(value);    
}, "请填写正确的IP地址。");

// 字符验证，只能包含中文、英文、数字、下划线等字符。    
jQuery.validator.addMethod("stringCheck", function(value, element) {       
return this.optional(element) || /^[a-zA-Z0-9\u4e00-\u9fa5-_]+$/.test(value);       
}, "只能包含中文、英文、数字、下划线等字符");   

// 匹配english  
jQuery.validator.addMethod("isEnglish", function(value, element) {       
return this.optional(element) || /^[A-Za-z]+$/.test(value);       
}, "匹配english");   

// 匹配汉字  
jQuery.validator.addMethod("isChinese", function(value, element) {       
return this.optional(element) || /^[\u4e00-\u9fa5]+$/.test(value);       
}, "匹配汉字");   

// 匹配中文(包括汉字和字符) 
jQuery.validator.addMethod("isChineseChar", function(value, element) {       
return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);       
}, "匹配中文(包括汉字和字符) "); 

// 判断是否为合法字符(a-zA-Z0-9-_)
jQuery.validator.addMethod("isRightfulString", function(value, element) {       
return this.optional(element) || /^[A-Za-z0-9_-]+$/.test(value);       
}, "判断是否为合法字符(a-zA-Z0-9-_)");   

// 判断是否包含中英文特殊字符，除英文"-_"字符外
jQuery.validator.addMethod("isContainsSpecialChar", function(value, element) {  
var reg = RegExp(/[(\ )(\`)(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\+)(\=)(\|)(\{)(\})(\')(\:)(\;)(\')(',)(\[)(\])(\.)(\<)(\>)(\/)(\?)(\~)(\！)(\@)(\#)(\￥)(\%)(\…)(\&)(\*)(\（)(\）)(\—)(\+)(\|)(\{)(\})(\【)(\】)(\‘)(\；)(\：)(\”)(\“)(\’)(\。)(\，)(\、)(\？)]+/);   
return this.optional(element) || !reg.test(value);       
}, "含有中英文特殊字符"); 

});
